package com.asp.aspchain.transaction.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;

import com.asp.aspchain.transaction.request.TranctionCommitReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.service.IAppBaseService;
import com.asp.aspchain.core.util.AjaxResult;
import com.asp.aspchain.core.util.BlogUtil;
import com.asp.aspchain.core.util.HttpRequestUtil;

/**
 * @author yangxiaotian
 * @date 2018年10月24日 下午2:18:27
 * @description:
 * @version 1.0
 *
 */
@RestController
@RequestMapping("/tranction")
public class AppCommitController {
	private static final Logger logger = LoggerFactory.getLogger(AppCommitController.class);
	@Autowired
	private IAppBaseService appBaseService;
	@Value("${server.port}")
	private String port;
	@Value("${server.context-path}")
	private String context;

	@RequestMapping(value = "commit", method = RequestMethod.POST)
	public String commitHandle(@RequestBody String json) {
		logger.info("==========交易接口开始==========");
		BlogUtil.inlog(json, "", "tranction/commit/", "commit", "req");
		TranctionCommitReq req = null;
		String reqsp = null;
		String tokenType = null;
		String url = null;
		String accountType = null;
		String type = null;
		try {
			req = JSON.parseObject(json, TranctionCommitReq.class);
		} catch (Exception e) {
			logger.error("交易接口解析错误：" + e.getMessage());
		}
		checkPayParam(req);
		String hmac = req.getHmac();
		boolean falg = sign(hmac);
		if (falg) {
			InetAddress addr = null;
			try {
				addr = InetAddress.getLocalHost();
			} catch (UnknownHostException e) {
				e.printStackTrace();
				logger.error(e.getMessage());
			}
			String ip = addr + port;
			String appid = req.getAppmark();
			AppBaseModel appBaseModel = appBaseService.selectById(appid);
			if (appBaseModel != null) {
				tokenType = appBaseModel.getTokentype();
				accountType = appBaseModel.getAccounttype();
				String eoa = "00";
				String coa = "01";
				if (eoa.equals(accountType)) {
					type = "eoa";
				} else if (coa.equals(accountType)) {
					type = "coa";
				}
			}
			url = "http://" + ip + "/" + context + "/tranction" + "/" + type + "/" + tokenType + "/commit";
			reqsp = HttpRequestUtil.postJson(url, json);
			BlogUtil.inlog(reqsp, "", "tranction/commit/" + tokenType, "commit", "resp");
		} else {
			logger.info("验证签名不通过");
		}
		logger.info("==========交易接口结束==========");
		return reqsp;
	}

	/**
	 * @Description: 入参校验
	 * @param: TranctionCommitReq
	 * @return:
	 * @throws :
	 */
	public AjaxResult checkPayParam(TranctionCommitReq req) {
		AjaxResult result = new AjaxResult();
		return result;
	}

	/**
	 * @Description: 签名校验
	 * @param: hmac
	 * @return:
	 * @throws :
	 */

	public boolean sign(String hmac) {
		return true;
	}
}
